package com.itheima.web;

import com.itheima.R;
import com.itheima.pojo.UmsAdmin;
import com.itheima.service.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class UmsAdminController {

    @Autowired
    private UmsAdminService umsAdminService;

    /**
     * 登录
     * @param param
     * @return
     */
    @PostMapping("login")
    public R login (@RequestBody Map<String,String> param){
        return umsAdminService.login(param);
    }

    /**
     * 登出
     * @param
     * @return
     */
    @PostMapping("logout")
    public R logout (HttpSession session){
        session.invalidate();
        return R.success(null);
    }

    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("info")
    public R info (){
        return umsAdminService.info();
    }

    /**
     * 分页查询用户信息
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("list")
    public R findByPage(@RequestParam (value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                        @RequestParam (value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
                        String keyword){
        return umsAdminService.findByPage(pageNum,pageSize,keyword);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public R deleteById(@PathVariable Integer id){
        return umsAdminService.deleteById(id);
    }

    /**
     * 添加用户
     * @param umsAdmin
     * @return
     */
    @PostMapping("register")
    public R saveUms(@RequestBody UmsAdmin umsAdmin){
        return umsAdminService.saveUms(umsAdmin);
    }

    /**
     * 获取用户角色
     * @param id
     * @return
     */
    @GetMapping("role/{id}")
    public R getRole(@PathVariable Integer id){
        return umsAdminService.getRole(id);
    }

    /**
     * 修改用户信息
     * @param id
     * @param umsAdmin
     * @return
     */
    @PostMapping("update/{id}")
    public R updateById(@PathVariable Integer id,@RequestBody UmsAdmin umsAdmin){
        return umsAdminService.updateById(id,umsAdmin);
    }

    /**
     * 修改用户状态
     * @param id
     * @param status
     */
    @PostMapping("updateStatus/{id}")
    public R updateStatus(@PathVariable Integer id,Integer status){
        return umsAdminService.updateStatus(id,status);
    }

    /**
     * 根据用户id查找用户
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getUserInfo(@PathVariable Integer id){
        return umsAdminService.fingById(id);
    }

    /**
     * 修改指定用户密码
     * @param param
     * @return
     */
    @PostMapping("updatePassword")
    public R updatePassword(@RequestBody Map<String,String> param){
        return umsAdminService.updatePassword(param);
    }

    /**
     * 刷新token
     * @return
     */
    @GetMapping("refreshToken")
    public R refreshToken(){
        return umsAdminService.refreshToken();
    }

    /**
     * 给用户分配角色
     * 接口地址:/admin/role/update
     * @param adminId 用户id
     * @param roleIds 分配的角色id数组
     * @return
     */
    @PostMapping("/role/update")
    public R updateAdminRole(@RequestParam("adminId") Long adminId,
                                      @RequestParam("roleIds") List<Long> roleIds){
        return umsAdminService.updateAdminRole(adminId, roleIds);
    }

}
